package framework.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import framework.bo.SysMenu;
import framework.bo.SysRoleMenu;
import framework.common.Common;
import framework.common.ResponseVo;
import framework.service.SysMenuService;
import framework.util.http._HttpRequestUtil;
import framework.util.layui.TreeVo;
import framework.util.layui._LayuiUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import frameworkExt.util.controller.BaseControllerExt;
import framework.service.SysRoleMenuService;
;import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/framework/SysRoleMenu.do")
public class SysRoleMenuController extends BaseControllerExt {

    @Resource
    private SysRoleMenuService service;
    @Resource
    private SysMenuService sysMenuService;

    /**
     * 根据角色主键，获取树状菜单列表。如果该角色有菜单的权限，则sysMenus.getCheck==true。
     */
    @ResponseBody
    @RequestMapping(params = "method=listMenuTreeFilterByRole")
    public List<TreeVo> listMenuTreeFilterByRole(HttpServletRequest request){
        String roleLid = request.getParameter("roleLid");
        if(StringUtils.isBlank(roleLid)){
            return null;
        }

        List<TreeVo> result = new ArrayList<>();
        Map<String, String> requestMap = _HttpRequestUtil.getRequestMap(request);
        requestMap.put("where_sm.plid_eq", "-1");
        List<SysMenu> list =  sysMenuService.listTreeFilterByRole(requestMap);
        for (SysMenu sysMenu : list) {
            setChildrenMenuFilterByRole(sysMenu, requestMap);
            TreeVo vo = _LayuiUtil.convertToTreeVo(sysMenu, JSONObject.parseObject("{id: 'lid', title: 'cname', children: 'childList', checked: 'checked'}"));
            result.add(vo);
        }

        return result;
    }

    private void setChildrenMenuFilterByRole(SysMenu sysMenu, Map<String, String> requestMap){
        if(Common.IS_NO == sysMenu.getHaschild()){
            return;
        }

        //设置子节点
        requestMap.put("where_sm.plid_eq", sysMenu.getLid().toString());
        List<SysMenu> children =  sysMenuService.listTreeFilterByRole(requestMap);
        sysMenu.setChildList(children);

        //递归子节点
        for (SysMenu childrenSysMenu : children) {
            setChildrenMenuFilterByRole(childrenSysMenu, requestMap);
        }
    }

    @ResponseBody
    @RequestMapping(params = "method=grantMenu")
    public ResponseVo grantMenu(HttpServletRequest request){
        String roleLid = request.getParameter("roleLid");
        String sysRoleMenuList = request.getParameter("sysRoleMenuList");
        if(StringUtils.isBlank(roleLid)){
            return sendFail("参数错误");
        }

        List<SysRoleMenu> list = JSON.parseObject(sysRoleMenuList, new TypeReference<List<SysRoleMenu>>(){});
        service.grantMenu(roleLid, list);

        return sendSuccess();
    }

}
